package com.ibeetl.cms.web;

import com.ibeetl.admin.console.service.DictConsoleService;
import com.ibeetl.admin.core.annotation.Function;
import com.ibeetl.admin.core.entity.CoreUser;
import com.ibeetl.admin.core.entity.Message;
import com.ibeetl.admin.core.file.FileService;
import com.ibeetl.admin.core.service.CorePlatformService;
import com.ibeetl.admin.core.service.MessageService;
import com.ibeetl.admin.core.util.ConvertUtil;
import com.ibeetl.admin.core.util.ValidateConfig;
import com.ibeetl.admin.core.web.JsonResult;
import com.ibeetl.cms.entity.*;
import com.ibeetl.cms.service.*;
import com.ibeetl.cms.util.BusinessTypeConstant;
import com.ibeetl.cms.util.ProcessConstant;
import com.ibeetl.cms.util.RoleConstant;
import com.ibeetl.cms.util.common;
import com.ibeetl.cms.web.query.MedicalCareQuery;
import com.ibeetl.cms.web.query2.MedicalCareEnterpriseQuery;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

/**
 * 医疗管理 接口
 */
@Controller
public class MedicalCareController{

    private final Log log = LogFactory.getLog(this.getClass());
    private static final String MODEL = "/cms/medicalCare";


    @Autowired private MedicalCareService medicalCareService;

    @Autowired
    FileService fileService;

    @Autowired
    CorePlatformService platformService;

    @Autowired private EnterpriseService enterpriseService;

    @Autowired
    private EssentialInformationService essentialInformationService;

    @Autowired
    private BusinessProcessService businessProcessService;


    @Autowired
    private  SincerityService sincerityService;

    @Autowired
    private MessageService messageService;


    private StateTable stateTable=new StateTable();
    /* 页面 */

    @GetMapping(MODEL + "/index.do")
    @ResponseBody
    public ModelAndView index(String roleName,String year) {
        ModelAndView view;
        if (roleName.equals("shenbao")) {
            view = new ModelAndView("/cms/medicalCare/index.html") ;
            view.addObject("search", MedicalCareQuery.class.getName());
        } else if (roleName.equals("shouli")) {
            view = new ModelAndView("/cms/medicalCare/enterpriseindex.html");
            view.addObject("search", MedicalCareEnterpriseQuery.class.getName());
        } else if (roleName.equals("chushen")) {
            view = new ModelAndView("/cms/medicalCare/chushenindex.html");
            view.addObject("search", MedicalCareEnterpriseQuery.class.getName());
        } else {
            view = new ModelAndView("/cms/medicalCare/fushenindex.html");
            view.addObject("search", MedicalCareEnterpriseQuery.class.getName());
        }

        if (roleName.equals("chushen") || roleName.equals("fushen")) {
            MedicalCareQuery condtion = new MedicalCareQuery();

            if(StringUtils.isEmpty(year)){
                Integer yearNum = Calendar.getInstance().get(Calendar.YEAR);
                year = yearNum.toString();
            }
            condtion.setYear(year.trim());
            view.addObject("year",year);

            /**
             * 获取本年度所有有申报的公司
             */
            List<MedicalCare> lists = medicalCareService.queryCompany(condtion);
            List<Map<String, Object>> companies = new ArrayList<>(16);
            for (MedicalCare medicalCare : lists) {
                if(medicalCare.getCompanyId() == null){
                    continue;
                }
                Map<String, Object> item = new HashMap<>(8);
                int waitCheckNumber = 0;
                int allCheckNumber = 0;
                if (roleName.equals("chushen")) {
                    waitCheckNumber = firstCheckNumber(Long.valueOf(medicalCare.getCompanyId()), "TODO",year);
                    allCheckNumber = firstCheckNumber(Long.valueOf(medicalCare.getCompanyId()), "ALL",year);
                }else{
                    waitCheckNumber = finalCheckNumber(Long.valueOf(medicalCare.getCompanyId()), "TODO",year);
                    allCheckNumber = finalCheckNumber(Long.valueOf(medicalCare.getCompanyId()), "ALL",year);
                }
                if(waitCheckNumber == 0){
                    continue;
                }
                item.put("companyId", medicalCare.getCompanyId());
                item.put("companyName", medicalCare.getCurrentWorkingEnterprise());
                item.put("waitCheckNumber", waitCheckNumber);
                item.put("allCheckNumber", allCheckNumber);
                companies.add(item);
            }
            view.addObject("companies", companies);

            view.addObject("roleName", roleName);
        }
        return view;
    }

    @PostMapping(MODEL + "/firstcheckList.json")
    @ResponseBody
    public JsonResult<PageQuery> firstcheckList(MedicalCareQuery condtion) {
        /**
         * 获得本年度已办任务
         */
        if(condtion.getYear()=="" || condtion.getYear()==null) {
            Integer year = Calendar.getInstance().get(Calendar.YEAR);
            condtion.setYear(year.toString());
        }
        condtion.setProcessUserId(getUserId());
        condtion.setBusinessType(BusinessTypeConstant.medicalCare);
        PageQuery page = condtion.getPageQuery();
        medicalCareService.queryDoneTask(page);
        return JsonResult.success(page);
    }

    /**
     * 初审获取该申报，该公司下的员工
     *
     * @param companyId
     * @return
     */
    @PostMapping(MODEL + "/employees.json")
    @Function("medicalCare.check")
    @ResponseBody
    public JsonResult<PageQuery> employees(String companyId,String year) {
        MedicalCareQuery condtion = new MedicalCareQuery();

        if(StringUtils.isEmpty(year)){
            Integer yearNum = Calendar.getInstance().get(Calendar.YEAR);
            year = yearNum.toString();
        }
        condtion.setYear(year.trim());
        condtion.setCompanyId(companyId);

        List<String> ids = new ArrayList();
        ids.add(ProcessConstant.FIRSTCHECK_TERMINIATE_STR);
        ids.add(ProcessConstant.FIRSTCHECK_STR);
        ids.add(ProcessConstant.FIRSTCHECK_ADOPT_STR);
        ids.add(ProcessConstant.COMPANY_ADOPT_STR);
        condtion.setIds(ids);

        PageQuery page = condtion.getPageQuery();
        medicalCareService.queryEmployees(page);
        return JsonResult.success(page);
    }

    /**
     * 复审获取该申报，该公司下的员工
     *
     * @param companyId
     * @return
     */
    @PostMapping(MODEL + "/fushenemployees.json")
    @Function("medicalCare.review")
    @ResponseBody
    public JsonResult<PageQuery> fushenemployees(String companyId,String year) {
        MedicalCareQuery condtion = new MedicalCareQuery();

        if(StringUtils.isEmpty(year)){
            Integer yearNum = Calendar.getInstance().get(Calendar.YEAR);
            year = yearNum.toString();
        }
        condtion.setYear(year.trim());
        condtion.setCompanyId(companyId);

        List<String> ids = new ArrayList();
        ids.add(ProcessConstant.FINALCHECK_TERMINIATE_STR);
        ids.add(ProcessConstant.FINALCHECK_BACK_STR);
        ids.add(ProcessConstant.FINALCHECK_ADOPT_STR);
        ids.add(ProcessConstant.FIRSTCHECK_ADOPT_STR);
        condtion.setIds(ids);

        PageQuery page = condtion.getPageQuery();
        medicalCareService.queryEmployees(page);
        return JsonResult.success(page);
    }


    @GetMapping(MODEL + "/shenhe.do")
    @ResponseBody
    public ModelAndView shenhe(Integer id, String roleName,String bStateCode) {
        ModelAndView view;
        if(bStateCode.equals(ProcessConstant.TO_FIRSTCHECK) && roleName.equals("chushen")){
            view= new ModelAndView("/cms/medicalCare/chushenedit.html");
        }else if(bStateCode.equals(ProcessConstant.TO_FINALCHECK) && roleName.equals("fushen")){
            view= new ModelAndView("/cms/medicalCare/fushenedit.html");
        }else{
            view  = new ModelAndView("/cms/medicalCare/findenterpriseedit.html");
            view.addObject("look",true);
        }

        MedicalCare medicalCare = medicalCareService.queryById(id);
        medicalCare.setRoleName(roleName);
        view.addObject("medicalCare", medicalCare);
        return view;
    }

    @GetMapping(MODEL + "/edit.do")
    @Function("medicalCare")
    @ResponseBody
    public ModelAndView edit(Integer id) {
        ModelAndView view = new ModelAndView("/cms/medicalCare/edit.html");
        MedicalCare medicalCare = medicalCareService.queryById(id);
        view.addObject("medicalCare", medicalCare);
        return view;
    }

    @GetMapping(MODEL + "/add.do")
    @Function("medicalCare")
    @ResponseBody
    public ModelAndView add() {
        ModelAndView view = new ModelAndView("/cms/medicalCare/add.html");
        EssentialInformation essentialInformation = essentialInformationService.finduserid(getUserId());
        MedicalCareQuery medicalCareQuery=new MedicalCareQuery();medicalCareQuery.setUserid(getUserId());
        int count=medicalCareService.queryEmployeesCount(medicalCareQuery);
        String judge="";
        switch (count){
            case 0: judge="第一年体检" ;break;
            case 1:judge="第二年体检" ;break;
            case 2:judge="第三年体检" ;break;
            case 3:judge="第四年体检" ;break;
            case 4: judge="第五年体检" ;break;
            default:
                ;
                break;
        }
        view.addObject("medicalCategory", judge);
        view.addObject("essentialInformation", essentialInformation);
        return view;
    }

    /* ajax json */

    @PostMapping(MODEL + "/list.json")
    @Function("medicalCare")
    @ResponseBody
    public JsonResult<PageQuery> list(MedicalCareQuery condtion)
    {
        Long userid = getUserId();
        condtion.setUserid(userid);
        if(condtion.getYear()=="" || condtion.getYear()==null){
            Integer year = Calendar.getInstance().get(Calendar.YEAR);
            condtion.setYear(year.toString());
        }
        PageQuery page = condtion.getPageQuery();
        medicalCareService.queryByCondition(page);
        return JsonResult.success(page);
    }

    /**
     * 受理页面数据
     * @param condtion
     * @return
     */
    @PostMapping(MODEL + "/Acceptancelist.json")
    @Function("medicalCare.company")
    @ResponseBody
    public JsonResult<PageQuery> Acceptancelist(MedicalCareQuery condtion) {
        condtion.setUserid(getUserId());
        if (StringUtils.isEmpty(condtion.getYear())) {
            Integer year = Calendar.getInstance().get(Calendar.YEAR);
            condtion.setYear(year.toString());
        }
        PageQuery page = condtion.getPageQuery();

        condtion.setCompanyId(getUserId().toString());
        if(condtion.getBusinessState()=="" || condtion.getBusinessState()==null){
            condtion.setBusinessState(ProcessConstant.TO_COMPANY);
        }else {
            condtion.setBusinessState(condtion.getBusinessState());
        }
        //查询企业数据
        medicalCareService.findenterprise(page);
        return JsonResult.success(page);
    }

    @Autowired
    private DictConsoleService dictConsoleService;
    /**
     * 新增（保存）（保存并申请）
     *
     * @param medicalCare
     * @return
     */
    @PostMapping(MODEL + "/add.json")
    @Function("medicalCare")
    @ResponseBody
    public JsonResult add(@Validated(ValidateConfig.ADD.class)MedicalCare medicalCare, String judge)
    {
Long userId = getUserId();
        String yearStr = dictConsoleService.getDeclareYear();
        medicalCare.setYear(yearStr);
        medicalCare.setUserId(userId.intValue());
        medicalCare.setCompanyId(Math.toIntExact(platformService.getCurrentUser().getCompanyId()));
        medicalCare.setCreationDate(new Date());
        common.common(stateTable,judge);
        medicalCare.setDateReporting(stateTable.getDate());
        medicalCare.setBusinessState(stateTable.getBusinessState());
        medicalCare.setBusinessStateCode(stateTable.getBusinessStateCode());
        medicalCareService.save(medicalCare);
        //调用增加业务流程数据
        businessProcessService.processInformation(medicalCare.getId(),stateTable.getBusinessState(),
                stateTable.getBusinessStateCode(),medicalCare.getHandlingSuggestion(),"成功"
                , BusinessTypeConstant.medicalCare);
        return new JsonResult().success();
    }
    /**
     * 编辑（保存）（保存并申请）
     *
     * @param medicalCare
     * @return
     */
    @PostMapping(MODEL + "/update.json")
    @Function("medicalCare")
    @ResponseBody
    public JsonResult<String> update(@Validated(ValidateConfig.UPDATE.class)  MedicalCare medicalCare,String judge) {
        common.common(stateTable,judge);
        medicalCare.setDateReporting(stateTable.getDate());
        medicalCare.setBusinessState(stateTable.getBusinessState());
        medicalCare.setBusinessStateCode(stateTable.getBusinessStateCode());
        boolean success = medicalCareService.updateTemplate(medicalCare);
        if (success) {
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }
    }



    @GetMapping(MODEL + "/view.json")
    @Function("cms.medicalCare.query")
    @ResponseBody
    public JsonResult<MedicalCare> queryInfo(Integer id) {
        MedicalCare medicalCare = medicalCareService.queryById( id);
        return  JsonResult.success(medicalCare);
    }

    @PostMapping(MODEL + "/delete.json")
    @Function("medicalCare")
    @ResponseBody
    public JsonResult delete(String ids) {
        if (ids.endsWith(",")) {
            ids = StringUtils.substringBeforeLast(ids, ",");
        }
        List<Long> idList = ConvertUtil.str2longs(ids);
        medicalCareService.batchDelMedicalCare(idList);
        return new JsonResult().success();
    }
    /**
     * 查看页面跳转
     *
     * @return
     */
    @GetMapping(MODEL + "/findenterprise.do")
    @ResponseBody
    public ModelAndView findenterprise(Integer id) {
        ModelAndView view = new ModelAndView("/cms/medicalCare/findenterpriseedit.html");
        MedicalCare medicalCare = medicalCareService.queryById(id);
        view.addObject("look",true);
        view.addObject("medicalCare", medicalCare);
        return view;
    }



    /**
     * 单位产业人才点击受理页面跳转
     *
     * @return
     */
    @GetMapping(MODEL + "/acceptance.do")
    @Function("medicalCare.company")
    @ResponseBody
    public ModelAndView acceptance(Integer id) {
        ModelAndView view = new ModelAndView("/cms/medicalCare/acceptance.html");
        MedicalCare medicalCare = medicalCareService.queryById(id);
        view.addObject("medicalCare", medicalCare);
        return view;
    }


    /**
     * 受理终止
     */
    @PostMapping(MODEL + "/Admissibility.json")
    @Function("medicalCare.company")
    @ResponseBody
    public JsonResult<String> Admissibility(@Validated(ValidateConfig.UPDATE.class) MedicalCare medicalCare) {
        medicalCare.setBusinessState(ProcessConstant.TERMINIATE_STR);
        medicalCare.setBusinessStateCode(ProcessConstant.TERMINIATE);
        medicalCare.setProcessResult(ProcessConstant.COMPANY_TERMINIATE_STR);
        boolean success = medicalCareService.updateTemplate(medicalCare);
        if (success) {
            //调用增加业务流程数据
            businessProcessService.processInformation(medicalCare.getId(),medicalCare.getBusinessState(),
                    medicalCare.getBusinessStateCode(),medicalCare.getHandlingSuggestion(),ProcessConstant.COMPANY_TERMINIATE_STR,BusinessTypeConstant.medicalCare);
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }
    }

    /**
     * 初审终止
     */
    @PostMapping(MODEL + "/FirstTrialTermination.json")
    @Function("medicalCare.check")
    @ResponseBody
    public JsonResult<String> FirstTrialTermination(@Validated(ValidateConfig.UPDATE.class) MedicalCare medicalCare) {
        medicalCare.setBusinessState(ProcessConstant.TERMINIATE_STR);
        medicalCare.setBusinessStateCode(ProcessConstant.TERMINIATE);
        medicalCare.setProcessResult(ProcessConstant.FIRSTCHECK_TERMINIATE_STR);
        //初审人和id
        CoreUser currentUser = platformService.getCurrentUser();
        medicalCare.setProcessUserId(currentUser.getId().intValue());
        medicalCare.setProcessUserName(currentUser.getName());
        boolean success = medicalCareService.updateTemplate(medicalCare);
        if (success) {
            //调用增加业务流程数据
            businessProcessService.processInformation(medicalCare.getId(),medicalCare.getBusinessState(),
                    medicalCare.getBusinessStateCode(),medicalCare.getHandlingSuggestion(),ProcessConstant.FIRSTCHECK_TERMINIATE_STR,BusinessTypeConstant.medicalCare);
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }

    }

    /**
     * 复审终止
     */
    @PostMapping(MODEL + "/RetrialTermination.json")
    @Function("medicalCare.review")
    @ResponseBody
    public JsonResult<String> RetrialTermination(@Validated(ValidateConfig.UPDATE.class) MedicalCare medicalCare) {
        medicalCare.setBusinessState(ProcessConstant.TERMINIATE_STR);
        medicalCare.setBusinessStateCode(ProcessConstant.TERMINIATE);
        medicalCare.setProcessResult(ProcessConstant.FINALCHECK_TERMINIATE_STR);
        //复审人和id
        CoreUser currentUser = platformService.getCurrentUser();
        medicalCare.setReviewUserId(currentUser.getId().intValue());
        medicalCare.setReviewUserName(currentUser.getName());
        boolean success = medicalCareService.updateTemplate(medicalCare);
        if (success) {
            //调用增加业务流程数据
            businessProcessService.processInformation(medicalCare.getId(),medicalCare.getBusinessState(),
                    medicalCare.getBusinessStateCode(),medicalCare.getHandlingSuggestion(),ProcessConstant.FINALCHECK_TERMINIATE_STR,BusinessTypeConstant.medicalCare);
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }
    }

    /**
     * 受理退回
     */
    @PostMapping(MODEL + "/AdmissibleReturn.json")
    @Function("medicalCare.company")
    @ResponseBody
    public JsonResult<String> AdmissibleReturn(@Validated(ValidateConfig.UPDATE.class) MedicalCare medicalCare) {
        medicalCare.setBusinessState(ProcessConstant.RETURN_DECLARATION_STR);
        medicalCare.setBusinessStateCode(ProcessConstant.RETURN_DECLARATION);
        medicalCare.setProcessResult(ProcessConstant.COMPANY_BACK_STR);
        boolean success = medicalCareService.updateTemplate(medicalCare);
        if (success) {
            //调用增加业务流程数据
            businessProcessService.processInformation(medicalCare.getId(),ProcessConstant.COMPANY_BACK_STR,
                    ProcessConstant.COMPANY_BACK,medicalCare.getHandlingSuggestion(),ProcessConstant.COMPANY_BACK_STR,BusinessTypeConstant.medicalCare);
            //给企业发一条退回信息
            Message message=new Message();
            message.setCompanyId(medicalCare.getCompanyId());
            message.setUserId(medicalCare.getUserId());
            message.setStatus(-1);
            message.setContent(medicalCare.getHandlingSuggestion());
            message.setType(Integer.parseInt(BusinessTypeConstant.medicalCare));
            message.setCreateDate(new Date());
            messageService.save(message);
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }

    }

    /**
     * 初审退回
     */
    @PostMapping(MODEL + "/initialReview.json")
    @Function("medicalCare.check")
    @ResponseBody
    public JsonResult<String> initialReview(@Validated(ValidateConfig.UPDATE.class) MedicalCare medicalCare) {
        medicalCare.setBusinessState(ProcessConstant.RETURN_DECLARATION_STR);
        medicalCare.setBusinessStateCode(ProcessConstant.RETURN_DECLARATION);
        medicalCare.setProcessResult(ProcessConstant.FIRSTCHECK_STR);
        //初审人和id
        CoreUser currentUser = platformService.getCurrentUser();
        medicalCare.setProcessUserId(currentUser.getId().intValue());
        medicalCare.setProcessUserName(currentUser.getName());
        boolean success = medicalCareService.updateTemplate(medicalCare);
        if (success) {
            //调用增加业务流程数据
            businessProcessService.processInformation(medicalCare.getId(),ProcessConstant.FIRSTCHECK_STR,
                    ProcessConstant.FIRSTCHECK,medicalCare.getHandlingSuggestion(),ProcessConstant.FIRSTCHECK_STR,BusinessTypeConstant.medicalCare);
            //给企业发一条退回信息
            Message message=new Message();
            message.setCompanyId(medicalCare.getCompanyId());
            message.setUserId(medicalCare.getUserId());
            message.setStatus(-1);
            message.setContent(medicalCare.getHandlingSuggestion());
            message.setType(Integer.parseInt(BusinessTypeConstant.medicalCare));
            message.setCreateDate(new Date());
            messageService.save(message);
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }
    }

    /**
     * 复审退回
     */
    @PostMapping(MODEL + "/Reexamination.json")
    @Function("medicalCare.review")
    @ResponseBody
    public JsonResult<String> Reexamination(@Validated(ValidateConfig.UPDATE.class) MedicalCare medicalCare) {
        medicalCare.setBusinessState(ProcessConstant.TO_FIRSTCHECK_STR);
        medicalCare.setBusinessStateCode(ProcessConstant.TO_FIRSTCHECK);
        medicalCare.setProcessResult(ProcessConstant.FINALCHECK_BACK_STR);
        //复审人和id
        CoreUser currentUser = platformService.getCurrentUser();
        medicalCare.setReviewUserId(currentUser.getId().intValue());
        medicalCare.setReviewUserName(currentUser.getName());
        boolean success = medicalCareService.updateTemplate(medicalCare);
        if (success) {
            //调用增加业务流程数据
            businessProcessService.processInformation(medicalCare.getId(),ProcessConstant.FINALCHECK_BACK_STR,
                    ProcessConstant.FINALCHECK_BACK,medicalCare.getHandlingSuggestion(),ProcessConstant.FINALCHECK_BACK_STR,BusinessTypeConstant.medicalCare);
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }
    }

    /**
     * 受理通过
     */
    @PostMapping(MODEL + "/AcceptanceOK.json")
    @Function("medicalCare.company")
    @ResponseBody
    public JsonResult<String> AcceptanceOK(@Validated(ValidateConfig.UPDATE.class) MedicalCare medicalCare) {
        medicalCare.setBusinessState(ProcessConstant.TO_FIRSTCHECK_STR);
        medicalCare.setBusinessStateCode(ProcessConstant.TO_FIRSTCHECK);
        medicalCare.setProcessResult(ProcessConstant.COMPANY_ADOPT_STR);

        boolean success = medicalCareService.updateTemplate(medicalCare);
        if (success) {
            //调用增加业务流程数据
            businessProcessService.processInformation(medicalCare.getId(),ProcessConstant.COMPANY_ADOPT_STR,
                    ProcessConstant.COMPANY_ADOPT,medicalCare.getHandlingSuggestion(),ProcessConstant.COMPANY_ADOPT_STR,BusinessTypeConstant.medicalCare);
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }
    }

    /**
     *  初审通过
     */
    @PostMapping(MODEL + "/FirstTrial.json")
    @Function("medicalCare.check")
    @ResponseBody
    public JsonResult<String> FirstTrial(@Validated(ValidateConfig.UPDATE.class) MedicalCare medicalCare) {
        String businessType = "14";
        List<Sincerity> sincerityList = sincerityService.blacklist(medicalCare.getUserId(), medicalCare.getCompanyId(), businessType);
        if (null != sincerityList && sincerityList.size() > 0) {
            //用户或用户所属企业已进入黑名单
            for (Sincerity sincerity : sincerityList) {
                if (sincerity.getType().equals(Long.toString(RoleConstant.PERSON))) {
                    //个人
                    return JsonResult.failMessage(medicalCare.getName() + "在黑名单中有记录");
                } else if (sincerity.getType().equals(Long.toString(RoleConstant.COMPANY))) {
                    //企业
                    String name = enterpriseService.findenterprise(medicalCare.getCompanyId()).getEnterpriseName();
                    return JsonResult.failMessage(name + "公司在黑名单中有记录");
                }
            }
        }

        medicalCare.setBusinessState(ProcessConstant.TO_FINALCHECK_STR);
        medicalCare.setBusinessStateCode(ProcessConstant.TO_FINALCHECK);
        medicalCare.setProcessResult(ProcessConstant.FIRSTCHECK_ADOPT_STR);
        //初审人和id
        CoreUser currentUser = platformService.getCurrentUser();
        medicalCare.setProcessUserId(currentUser.getId().intValue());
        medicalCare.setProcessUserName(currentUser.getName());
        boolean success = medicalCareService.updateTemplate(medicalCare);
        if (success) {
            //调用增加业务流程数据
            businessProcessService.processInformation(medicalCare.getId(),ProcessConstant.FIRSTCHECK_ADOPT_STR,
                    ProcessConstant.FIRSTCHECK_ADOPT,medicalCare.getHandlingSuggestion(),ProcessConstant.FIRSTCHECK_ADOPT_STR,BusinessTypeConstant.medicalCare);
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }
    }

    /**
     * 复审通过
     */
    @PostMapping(MODEL + "/ReviewThrough.json")
    @Function("medicalCare.review")
    @ResponseBody
    public JsonResult<String> ReviewThrough(@Validated(ValidateConfig.UPDATE.class) MedicalCare medicalCare) {
        medicalCare.setBusinessState(ProcessConstant.DONE_STR);
        medicalCare.setBusinessStateCode(ProcessConstant.DONE);
        medicalCare.setProcessResult(ProcessConstant.FINALCHECK_ADOPT_STR);
        //复审人和id
        CoreUser currentUser = platformService.getCurrentUser();
        medicalCare.setReviewUserId(currentUser.getId().intValue());
        medicalCare.setReviewUserName(currentUser.getName());
        boolean success = medicalCareService.updateTemplate(medicalCare);
        if (success) {
            //调用增加业务流程数据
            businessProcessService.processInformation(medicalCare.getId(),ProcessConstant.FINALCHECK_ADOPT_STR,
                    ProcessConstant.FINALCHECK_ADOPT,medicalCare.getHandlingSuggestion(),ProcessConstant.FINALCHECK_ADOPT_STR,BusinessTypeConstant.medicalCare);
            return new JsonResult().success();
        } else {
            return JsonResult.failMessage("保存失败");
        }
    }

    @PostMapping(MODEL + "/batchFinalCheckPass.json")
    @Function("medicalCare.review")
    @ResponseBody
    public JsonResult<String> batchFinalCheckPass(String ids,String year) {
        /**
         * 更改状态为生效
         */
        List<Long> idList = ConvertUtil.str2longs(ids);
        for(Long id : idList){
            /**
             * 获得这个公司id下所有待复审的业务
             */
            MedicalCareQuery condtion = new MedicalCareQuery();

            if(StringUtils.isEmpty(year)){
                Integer yearNum = Calendar.getInstance().get(Calendar.YEAR);
                year = yearNum.toString();
            }
            condtion.setYear(year.trim());
            condtion.setCompanyId(String.valueOf(id));

            List<String> params = new ArrayList();
            params.add(ProcessConstant.FIRSTCHECK_ADOPT_STR);
            condtion.setIds(params);
            PageQuery page = condtion.getPageQuery();
            page.setPageSize(Integer.MAX_VALUE);
            medicalCareService.queryEmployees(page);
            List<MedicalCare> medicalCareList = page.getList();

            for(MedicalCare medicalCare : medicalCareList){
                medicalCare.setBusinessState(ProcessConstant.DONE_STR);
                medicalCare.setBusinessStateCode(ProcessConstant.DONE);
                medicalCare.setProcessResult(ProcessConstant.FINALCHECK_ADOPT_STR);
                //复审人和id
                CoreUser currentUser = platformService.getCurrentUser();
                medicalCare.setReviewUserId(currentUser.getId().intValue());
                medicalCare.setReviewUserName(currentUser.getName());
                boolean success = medicalCareService.updateTemplate(medicalCare);
                if (success) {
                    //调用增加业务流程数据
                    businessProcessService.processInformation(medicalCare.getId(),ProcessConstant.FINALCHECK_ADOPT_STR,
                            ProcessConstant.FINALCHECK_ADOPT,medicalCare.getHandlingSuggestion(),ProcessConstant.FINALCHECK_ADOPT_STR,BusinessTypeConstant.medicalCare);
                } else {
                    return JsonResult.failMessage("保存失败");
                }
            }
        }
        return JsonResult.success();
    }

// 人才公共方法(所有)

    /**
     * 公共方法
     * 用于获取登录ID
     *
     * @return
     */
    public Long getUserId() {
        CoreUser currentUser = platformService.getCurrentUser();
        Long userId = currentUser.getId();
        return userId;
    }

    /**
     * 获取初审的数量
     */
    private int firstCheckNumber(Long companyId, String type,String year) {
        /**
         * 获取待初审的数量,type = 'TODO'
         */

        MedicalCareQuery condition = new MedicalCareQuery();

        if(StringUtils.isEmpty(year)){
            Integer yearNum = Calendar.getInstance().get(Calendar.YEAR);
            year = yearNum.toString();
        }
        condition.setYear(year.trim());
        condition.setCompanyId(String.valueOf(companyId));

        List<String> ids = new ArrayList();
        if ("TODO".equalsIgnoreCase(type)) {
            ids.add(ProcessConstant.COMPANY_ADOPT_STR);
            ids.add(ProcessConstant.FINALCHECK_BACK_STR);
        } else {
            ids.add(ProcessConstant.FIRSTCHECK_TERMINIATE_STR);
            ids.add(ProcessConstant.FIRSTCHECK_STR);
            ids.add(ProcessConstant.FIRSTCHECK_ADOPT_STR);
            ids.add(ProcessConstant.COMPANY_ADOPT_STR);
            ids.add(ProcessConstant.FINALCHECK_BACK_STR);
        }

        condition.setIds(ids);
        int size = medicalCareService.queryEmployeesCount(condition);
        return size;
    }

    /**
     * 获取复审的数量
     */
    private int finalCheckNumber(Long companyId, String type,String year) {
        /**
         * 获取待初审的数量,type = 'TODO'
         */

        MedicalCareQuery condition = new MedicalCareQuery();

        if(StringUtils.isEmpty(year)){
            Integer yearNum = Calendar.getInstance().get(Calendar.YEAR);
            year = yearNum.toString();
        }
        condition.setYear(year.trim());
        condition.setCompanyId(String.valueOf(companyId));

        List<String> ids = new ArrayList();
        if ("TODO".equalsIgnoreCase(type)) {
            ids.add(ProcessConstant.FIRSTCHECK_ADOPT_STR);
        } else {
            ids.add(ProcessConstant.FIRSTCHECK_ADOPT_STR);

            ids.add(ProcessConstant.FINALCHECK_TERMINIATE_STR);
            ids.add(ProcessConstant.FINALCHECK_BACK_STR);
            ids.add(ProcessConstant.FINALCHECK_ADOPT_STR);
        }

        condition.setIds(ids);
        int size = medicalCareService.queryEmployeesCount(condition);
        return size;
    }
}
